#include <QtCore/QDataStream>
#include <QyDebug.h>
#include "QpwGeometry.h"

using namespace QpwWorld;

/**************************************************************************************************/

Geometry::Geometry( const QUuid & uuid ) : QpwXML::Row(uuid) {

    init();

}

Geometry::Geometry( quint32 size, quint32 childCount, const QUuid & uuid )
    : QPolygonF(size), QpwXML::Row(uuid), subGeometry(childCount)
{

    init();

}

bool Geometry::isCircle() const {

    return p_isCircle;
}

qreal Geometry::radius() const {

    return p_radius;
}

QByteArray Geometry::toByteArray() const {

    QByteArray data;
    QDataStream ds( &data, QIODevice::WriteOnly );
    int count = Geometry::count();

    if ( count > 8 )
        count = 8;

    ds << center;
    ds << p_radius;
    ds << p_isCircle;
    ds << quint8(count);

    for ( int i = 0; i < count; ++i ) {

        ds << qreal( (*this)[i].x() );
        ds << qreal( (*this)[i].y() );

    }

    count = subGeometry.count();
    if ( count > 256 )
        count = 256;

    ds << quint8(count);

    for ( int i = 0; i < count; ++i ) {

        ds << subGeometry[i].toByteArray();

    }

    return data;
}

void Geometry::setRadius( qreal radius ) {

    p_radius = radius;
    p_isCircle = true;

}

void Geometry::dropCircleFlag() {

    p_isCircle = false;

}

bool Geometry::fromByteArray( QByteArray data ) {

    QDataStream ds( &data, QIODevice::ReadOnly );

    quint8 count, i;

    ds >> center;
    ds >> p_radius;
    ds >> p_isCircle;
    ds >> count;

//     QyDbgLocation();
//     QyDbgValue(uuid);
//     QyDbgValue(center);
//     QyDbgValue(p_radius);
//     QyDbgValue(p_isCircle);
//     QyDbgValue(count);

    if ( count > 8 ) {

        qWarning("Geometry: points count more 8");

        return false;
    }

    resize( int(count) );
    qreal x, y;

    for ( i = 0; i < count; ++i ) {

        ds >> x;
        ds >> y;
        (*this)[i].setX(x);
        (*this)[i].setY(y);

    }

    ds >> count;

    subGeometry.resize( int(count) );
    QByteArray subData;

    for ( i = 0; i < count; ++i ) {

        ds >> subData;
        if ( !subGeometry[i].fromByteArray(subData) )
            return false;

    }

    return true;
}

QpwXML::Values Geometry::values() const {

    QpwXML::Values values;
    values["name"] = name;
    values["geometry"] = toByteArray().toHex();

    return values;
}

void Geometry::setValues( const QpwXML::Values & values ) {

    name = values["name"].toString();
    fromByteArray( QByteArray::fromHex( values["geometry"].toByteArray() ) );

}

void Geometry::clear() {

    QPolygonF::clear();
    p_radius = 0;
    p_isCircle = false;
    center = QPointF();
    subGeometry.clear();

}

void Geometry::clearPolygon() {

    QPolygonF::clear();

}

/*static */QPointF calcCenter( const QPolygonF & polygon ) {

    return polygon.boundingRect().center();
}

void Geometry::init() {

    p_isCircle = false;

}
